//
//  LoginVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/17.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLALoginVC.h"
#import "MLATabBarVC.h"
#import "MLForgetVC.h"
#import "MLForgetPhoneVC.h"

@interface MLALoginVC ()

@end

@implementation MLALoginVC {
    UITextField *phoneTxf;
    UITextField *passowrdTxf;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addSubviewsInView];

}

#pragma - actions
- (void)commitBtnAction {
#pragma mark 登录
    CX_Log(@"登录");
    
    if (phoneTxf.text.length == 0) {
        [MLAHudManage showMessage:@"请输入账号"];
        return;
    }
    
    if (passowrdTxf.text.length == 0) {
        [MLAHudManage showMessage:@"请输入密码"];
        return;
    }
    
    NSDictionary *params = @{@"username":phoneTxf.text,
                             @"password":passowrdTxf.text};
    
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kApiLogin params:params success:^(id  _Nonnull response, NSString *_Nonnull msg) {
        [CX_GetUserDefaults() setObject:response[kAccessToken] forKey:kAccessToken];
        [CX_GetUserDefaults() setObject:response[@"is_shop"] forKey:@"isShop"];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MLAHudManage hideLoading];
            MLATabBarVC *rootVC = [[MLATabBarVC alloc] init];
            CX_GetKeyWindow().rootViewController = rootVC;
        });
    } failure:^{
    
    }];

}

#pragma - mark subviews
- (void)addSubviewsInView {
    UIImageView *iconImgv = [[UIImageView alloc] init];
    iconImgv.image = [UIImage imageNamed:@"login_icon"];
    [self.view addSubview:iconImgv];
    [iconImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(143);
        make.height.mas_equalTo(40);
        make.top.centerX.equalTo(self.view);
    }];
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.font = CX_GetNormalFont(16);
    titleLbl.textColor = CX_GetColorTitle();
    titleLbl.text = @"很高兴为您服务";
    [self.view addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(iconImgv.mas_bottom).offset(29);
    }];
    
    UIImageView *phoneImgv = [[UIImageView alloc] init];
    phoneImgv.image = [UIImage imageNamed:@"login_phone"];
    [self.view addSubview:phoneImgv];
    [phoneImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLbl.mas_bottom).offset(74);
        make.left.mas_equalTo(30);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(18);
    }];
    
    UILabel *phoneTitleLbl = [[UILabel alloc] init];
    phoneTitleLbl.font = CX_GetNormalFont(14);
    phoneTitleLbl.textColor = CX_GetColorHex(0x4A4A4A, 1);
    phoneTitleLbl.text = @"账号";
    [self.view addSubview:phoneTitleLbl];
    [phoneTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgv);
        make.left.equalTo(phoneImgv.mas_right).offset(15);
    }];
    
    phoneTxf = [[UITextField alloc] init];
    phoneTxf.textColor = CX_GetColorHex(0x4A4A4A, 1);
    phoneTxf.placeholder = @"输入你的账号";
    phoneTxf.font = CX_GetNormalFont(16);
    [self.view addSubview:phoneTxf];
    [phoneTxf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(45);
        make.height.mas_equalTo(30);
        make.top.equalTo(phoneImgv.mas_bottom).offset(13);
    }];
    
    UIView *line1View = [[UIView alloc] init];
    line1View.backgroundColor = CX_GetColorLine();
    [self.view addSubview:line1View];
    [line1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(0.5);
        make.top.equalTo(self->phoneTxf.mas_bottom).offset(14);
    }];
    
    UIImageView *passowrdImgv = [[UIImageView alloc] init];
    passowrdImgv.image = [UIImage imageNamed:@"login_lock"];
    [self.view addSubview:passowrdImgv];
    [passowrdImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1View.mas_bottom).offset(20);
        make.left.mas_equalTo(30);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(18);
    }];
    
    UILabel *pwdTitleLbl = [[UILabel alloc] init];
    pwdTitleLbl.font = CX_GetNormalFont(14);
    pwdTitleLbl.textColor = CX_GetColorHex(0x4A4A4A, 1);
    pwdTitleLbl.text = @"密码";
    [self.view addSubview:pwdTitleLbl];
    [pwdTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(passowrdImgv);
        make.left.equalTo(passowrdImgv.mas_right).offset(15);
    }];
    
    passowrdTxf = [[UITextField alloc] init];
    passowrdTxf.textColor = CX_GetColorHex(0x4A4A4A, 1);
    passowrdTxf.placeholder = @"输入你的密码";
    passowrdTxf.secureTextEntry = YES;
    passowrdTxf.font = CX_GetNormalFont(16);
    [self.view addSubview:passowrdTxf];
    [passowrdTxf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(45);
        make.height.mas_equalTo(30);
        make.top.equalTo(pwdTitleLbl.mas_bottom).offset(13);
    }];
    
    UIView *line2View = [[UIView alloc] init];
    line2View.backgroundColor = CX_GetColorLine();
    [self.view addSubview:line2View];
    [line2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(0.5);
        make.top.equalTo(self->passowrdTxf.mas_bottom).offset(14);
    }];
    
    UIButton *commitBtn = [self getCommitButtonWithTitle:@"登录"];
    [commitBtn addTarget:self action:@selector(commitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(40);
        make.top.equalTo(line2View.mas_bottom).offset(80);
    }];
    
    
    UIButton *forgetBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [forgetBtn1 setTitle:@"邮箱找回" forState:UIControlStateNormal];
    [forgetBtn1 setTitleColor:CX_GetColorBlue() forState:UIControlStateNormal];
    forgetBtn1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    forgetBtn1.titleLabel.font = [UIFont systemFontOfSize:14];
    [forgetBtn1 addTarget:self action:@selector(forgetBtnAction1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn1];
    [forgetBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line2View);
        make.top.equalTo(line2View.mas_bottom).offset(10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
    UIButton *forgetBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [forgetBtn2 setTitle:@"手机号找回" forState:UIControlStateNormal];
    [forgetBtn2 setTitleColor:CX_GetColorBlue() forState:UIControlStateNormal];
    forgetBtn2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    forgetBtn2.titleLabel.font = [UIFont systemFontOfSize:14];
    [forgetBtn2 addTarget:self action:@selector(forgetBtnAction2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn2];
    [forgetBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(line2View);
        make.top.equalTo(line2View.mas_bottom).offset(10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];

}

- (void)forgetBtnAction1 {
    MLForgetVC *forgetVC = [[MLForgetVC alloc] init];
    [self.navigationController pushViewController:forgetVC animated:YES];
}

- (void)forgetBtnAction2 {
    MLForgetPhoneVC *forgetVC = [[MLForgetPhoneVC alloc] init];
    [self.navigationController pushViewController:forgetVC animated:YES];
}


@end
